package com.bxd.baodan.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bxd.baodan.R;
import com.bxd.baodan.item.CarInsuranceItem;
import com.bxd.baodan.item.ViewHolder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class CheckboxAdapter extends BaseAdapter {
	// 填充数据的list
	public List<Map<String, Object>> list;
	private CarInsuranceItem carInsuer;
	private int type;// 1,2,3分别代表
	// private ArrayList<String> list;
	// 用来控制CheckBox的选中状况
	public static HashMap<Integer, Boolean> isSelected;
	// 上下文
	private Context context;
	// 用来导入布局
	private LayoutInflater inflater = null;
	private boolean state;

	/**
	 * @return the carInsuer
	 */
	public CarInsuranceItem getCarInsuer() {
		return carInsuer;
	}
	/**
	 * @param carInsuer the carInsuer to set
	 */
	public void setCarInsuer(CarInsuranceItem carInsuer) {
		this.carInsuer = carInsuer;
	}
	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.type = type;
		this.notifyDataSetChanged();
	}
	public CheckboxAdapter(List<Map<String, Object>> list, Context context,CarInsuranceItem carInsuer,int type) {
		this.context = context;
		this.list = list;
		this.type = type;
		this.carInsuer = carInsuer;
		inflater = LayoutInflater.from(context);
		isSelected = new HashMap<Integer, Boolean>();
		// 初始化数据
		initDate(false);
	}
	public CheckboxAdapter(List<Map<String, Object>> list, Context context) {
		this.context = context;
		this.list = list;
//		this.state = state;
		inflater = LayoutInflater.from(context);
		isSelected = new HashMap<Integer, Boolean>();
		// 初始化数据
		initDate(false);
	}

	// 初始化isSelected的数据
	private void initDate(boolean selected) {
		for (int i = 0; i < list.size(); i++) {
			getIsSelected().put(i, selected);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			// 获得ViewHolder对象
			holder = new ViewHolder();
			// 导入布局并赋值给convertview
			convertView = inflater.inflate(R.layout.item_checkbox, null);
			holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
			holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
			holder.tv_cb = (TextView) convertView.findViewById(R.id.item_tv_cb);
			// 为view设置标签
			convertView.setTag(holder);
		} else {
			// 取出holder
			holder = (ViewHolder) convertView.getTag();
		}
		// 设置list中TextView的显示
		holder.tv.setText(list.get(position).get("text").toString());
		// 根据isSelected来设置checkbox的选中状况
		if (list.get(position).get("b").toString().equals("true")) {
			holder.tv_cb.setVisibility(View.GONE);
			holder.cb.setVisibility(View.VISIBLE);
//			holder.cb.setChecked(getIsSelected().get(position));
			if (state) {
				boolean isSelected = false;
				 if(type == 1){
					 switch(position){
					 case 1:
						 //不计免赔特约险
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_MIANPEI, CarInsuranceItem.INSURE_KIND_MONEY);
						 break;
					 case 2:
						 //无过责任险
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_WUGUO, CarInsuranceItem.INSURE_KIND_MONEY);
						 break;
					default :
						break;
					 }
				 }else if(type == 2){
					 switch(position){
					 case 0:
						 //车辆损失险
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_CARSUNSHI, CarInsuranceItem.INSURE_KIND_MONEY);
						 if(carInsuer.isSelect(CarInsuranceItem.INSURETYPE_CARZIRAN,  CarInsuranceItem.INSURE_KIND_MONEY)){
							 isSelected = true;
							 //设置不可编辑
							 holder.cb.setEnabled(false); 
						 }else{
							 //可以编辑
							 holder.cb.setEnabled(true); 
						 }
						 break;
					 case 1:
						 //车辆被盗
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_CARBEIDAO, CarInsuranceItem.INSURE_KIND_MONEY);
						 break;
					default :
						break;
					 }
				 }else if(type == 3){
					 switch(position){
					 //自燃损失险
					 case 0:
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_CARZIRAN, CarInsuranceItem.INSURE_KIND_MONEY);
						 break;
					 //车上人员责任险
					 case 1:
						 isSelected =  carInsuer.isSelect(CarInsuranceItem.INSURETYPE_CARRENYUAN, CarInsuranceItem.INSURE_KIND_MONEY);
						 break;
					default :
						break;
					 }
				 }
				holder.cb.setChecked(isSelected);
			} else {
				holder.cb.setChecked(false);
			}
		}else{
			holder.tv_cb.setVisibility(View.VISIBLE);
			 if(type == 1){
				 holder.tv_cb.setText("20万元");
				 holder.tv_cb.setTextColor(context.getResources().getColor(R.color.green_btn));
			 }else{
				 holder.tv_cb.setText("不购买");
				 holder.tv_cb.setTextColor(context.getResources().getColor(R.color.text_gray));
			 }
			holder.cb.setVisibility(View.GONE);
		}
		return convertView;
	}

	public static HashMap<Integer, Boolean> getIsSelected() {
		return isSelected;
	}

	public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
		CheckboxAdapter.isSelected = isSelected;
	}
	
	public void changChecked(boolean isSelected) {
//		initDate(isSelected);
		state = isSelected;
		notifyDataSetChanged();
	}

}
